package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.LGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LAccountMapper;
import com.ruoyi.system.domain.LAccount;
import com.ruoyi.system.service.ILAccountService;

import javax.validation.Validator;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 账户Service业务层处理
 *
 * @author zyb
 * @date 2022-01-02
 */
@Service
public class LAccountServiceImpl extends ServiceImpl<LAccountMapper, LAccount> implements ILAccountService {

    @Autowired
    protected Validator validator;
    /**
     * 查询账户
     *
     * @param accid 账户主键
     * @return 账户
     */
    @Override
    public LAccount selectLAccountByAccid(Long accid) {
        return baseMapper.selectById(accid);
    }

    /**
     * 查询账户列表
     *
     * @param lAccount 账户
     * @return 账户
     */
    @Override
    public List<LAccount> selectLAccountList(LAccount lAccount) {
        return baseMapper.selectLAccountList(lAccount);
    }

    /**
     * 新增账户
     *
     * @param lAccount 账户
     * @return 结果
     */
    @Override
    public int insertLAccount(LAccount lAccount) {
        lAccount.setCreateBy(String.valueOf(getUserId()));
        lAccount.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(lAccount);
    }

    /**
     * 修改账户
     *
     * @param lAccount 账户
     * @return 结果
     */
    @Override
    public int updateLAccount(LAccount lAccount) {
        lAccount.setUpdateBy(String.valueOf(getUserId()));
        lAccount.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(lAccount);
    }

    /**
     * 批量删除账户
     *
     * @param accids 需要删除的账户主键
     * @return 结果
     */
    @Override
    public int deleteLAccountByAccids(Long[] accids) {
        return baseMapper.deleteLAccountByAccids(accids);
    }

    /**
     * 删除账户信息
     *
     * @param accid 账户主键
     * @return 结果
     */
    @Override
    public int deleteLAccountByAccid(Long accid) {
        return baseMapper.deleteById(accid);
    }

    @Override
    public String importAccount(List<LAccount> accountList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(accountList) || accountList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (LAccount account : accountList)
        {
            try
            {
                // 验证是否存在这个用户
                LAccount a=this.baseMapper.selectOne(new LambdaQueryWrapper<LAccount>().eq(LAccount::getCardnumber,account.getCardnumber()));
                if (StringUtils.isNull(a))
                {
                    BeanValidators.validateWithException(validator, account);
                    this.insertLAccount(account);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + account.getCardnumber()+ " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, account);
                    account.setAccid(a.getAccid());
                    this.updateLAccount(account);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " +  account.getCardnumber() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " +  account.getCardnumber() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " +  account.getCardnumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
